package com.cloudfast.platform.region;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloudfast.common.page.Page;
import com.cloudfast.common.result.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloudfast.common.user.UserUtils;
import com.cloudfast.context.error.CloudException;
import com.cloudfast.context.error.ErrorCode;
import com.cloudfast.controller.BaseController;
import com.cloudfast.platform.region.entity.RegionCity;
import com.cloudfast.platform.region.service.RegionService;

/**
 * 行政区域
 *
 * @author liuyw
 */
@RequestMapping(value = "/region/")
@RestController
@Api(value = "行政区域", tags = "行政区域")
public class RegionCityController extends BaseController<RegionCity> {

    @Autowired
    private RegionService regionService;

    @Override
    public ApiResponse findPage(Page<RegionCity> page, RegionCity entity) {
        return ApiResponse.success(regionService.findPage(page, entity));
    }

    @Override
    public ApiResponse findOne(Long id) {
        try {
            return ApiResponse.success(this.regionService.findOne(id));
        } catch (Exception e) {
            throw new CloudException(ErrorCode.FIND_ONE_ERROR);
        }
    }

    /**
     * 获取行政区数据地图; 默认显示子区域_full
     *
     * @param region
     * @return
     */
    @ApiOperation("获取区域地图json名称")
    @ApiImplicitParams({@ApiImplicitParam(name = "region", value = "区域名称", required = true)})
    @PostMapping(value = "regionCityMap")
    public Map<String, Object> regionCityMap(String region) {
        RegionCity regionCity = new RegionCity();
        regionCity.setRegionName(UserUtils.getUser().getRegionName());
        regionCity.setRegionCode(UserUtils.getUser().getRegionCode());
        if (StringUtils.isNotBlank(region)) {
            if (region.equals("china")) {
                region = "中国";
            }
            regionCity = this.regionService.getRegionCity(region);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("regionCode", regionCity.getRegionCode() + ".json");
        List<RegionCity> list = this.regionService.selectTree(regionCity.getRegionCode() + "");
        if (list != null && list.size() > 0) {
            map.put("regionCode", regionCity.getRegionCode() + "_full.json");
        }
        return map;
    }

    @ApiOperation("加载区域树tree-select")
    @ApiImplicitParams({@ApiImplicitParam(name = "parentCode", value = "父类id", required = false)})
    @PostMapping(value = "selectTree")
    public ApiResponse selectTree(String parentCode) {
        List<RegionCity> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(parentCode)) {
            list = regionService.selectTree(parentCode);
        } else {
            RegionCity regionCity = regionService.findOne(UserUtils.getUser().getRegionCode());
            list.add(regionCity);
        }

        return ApiResponse.success(list);
    }

}
